package com.AI;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import Communication.Communication;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class ARAI_Cam extends Activity {
	private static final int TAKE_IMAGE = 1;
	private static String lastFile;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_cam);
		
		findViewById(R.id.takeImage).setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				takePicture();
			}
		});
	}
	
	
	private void takePicture()
	{
		final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
	    lastFile = sdf.format(cal.getTime());
	    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File("/sdcard/tmp/" + lastFile + ".bmp")));
		startActivityForResult(intent, TAKE_IMAGE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode == RESULT_OK)
		{
			switch(requestCode){
			case TAKE_IMAGE:
				final File file = new File("/sdcard/tmp/" + lastFile + ".bmp");
				
				try {
					//Bitmap captureBmp = Media.getBitmap(getContentResolver(),Uri.fromFile(file));
					//FileOutputStream out = new FileOutputStream(new File("/sdcard/tmp/compressed.png"));
					//captureBmp.compress(Bitmap.CompressFormat.PNG, 100, out);

					//out.flush();
					//out.close();
					if (Communication.sendImage(file, lastFile) == 1)
					{
						Log.d("IMAGE COMMUNICATION", "SUCCES!");
						Toast.makeText(this, "Photo met succes verzonden!", Toast.LENGTH_LONG).show();
					}
					else{
						Toast.makeText(this, "Photo verzenden mislukt!!", Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					Log.d("Exception!", "Exception: " + e.getMessage());					
				}
				
				break;
			}
		}
	}


}
